package src;

import javax.swing.*;
import java.awt.*;

public class DepositDialog extends JDialog {
    private JTextField typeIdField;
    private JTextField nameField;
    private JTextArea descriptionArea;
    private boolean confirmed = false;

    public DepositDialog(Frame owner, String title) {
        super(owner, title, true);
        setSize(400, 300);
        setLocationRelativeTo(owner);

        JPanel panel = new JPanel(new GridLayout(4, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 添加输入字段
        panel.add(new JLabel("业务类型编号:"));
        typeIdField = new JTextField();
        panel.add(typeIdField);

        panel.add(new JLabel("业务名称:"));
        nameField = new JTextField();
        panel.add(nameField);

        panel.add(new JLabel("业务描述:"));
        descriptionArea = new JTextArea(3, 20);
        descriptionArea.setLineWrap(true);
        descriptionArea.setWrapStyleWord(true);
        JScrollPane scrollPane = new JScrollPane(descriptionArea);
        panel.add(scrollPane);

        // 添加按钮
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel);

        // 添加事件监听器
        confirmButton.addActionListener(e -> {
            if (validateInput()) {
                confirmed = true;
                dispose();
            }
        });

        cancelButton.addActionListener(e -> dispose());

        add(panel);
    }

    private boolean validateInput() {
        if (typeIdField.getText().trim().isEmpty() ||
            nameField.getText().trim().isEmpty() ||
            descriptionArea.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有字段！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public String getTypeId() {
        return typeIdField.getText().trim();
    }

    public String getName() {
        return nameField.getText().trim();
    }

    public String getDescription() {
        return descriptionArea.getText().trim();
    }
} 